"""
处理全局变量中没有的值，用函数调用，再设置为全局变量
"""
import time
import uuid
from faker import Faker
from settings import sql_settings
from tools.global_data import GlobalData
from tools.my_sql import MyMySQL
from loguru import logger

__all__ = ["current_time", "gen_uuid", "new_phone"]


def current_time():
    return str(int(time.time() * 1000))


def gen_uuid():
    return uuid.uuid4()


def new_phone():
    faker = Faker("zh_CN")
    mmysql = MyMySQL(**sql_settings)
    while True:
        new_phone = faker.phone_number()
        # 数据库查询是否注册过，count=0
        count = mmysql.get_count_from_database(f'SELECT * FROM tz_user WHERE user_mobile={new_phone}')
        if count == 0:
            logger.info("手机号未注册可以使用")
            break
    mmysql.close()
    # 设置new_phone为全局变量，供后面的接口使用
    setattr(GlobalData, "new_phone", new_phone)
    return new_phone

